<template>
  <div p4 style="height: 100%;" of-auto>
    <ElCard mb>
      <div mb>Python Helper 配置</div>
      <div v-if="!installed">
        <span>没有检测到Python Helper安装, </span>
        <ElButton type="primary" link @click="installHelper">点此</ElButton>
        <span>安装</span>
      </div>
      <div v-else>
        <div mb>Python Helper 已安装</div>
        <div mb>
          <ElButton type="primary" @click="onTestHelloworld">helloworld测试</ElButton>
          <ElButton type="primary" @click="onTestSleep">Sleep测试</ElButton>
          <ElButton type="primary" @click="onTestError">Error测试</ElButton>
          <ElButton type="primary" @click="onTestProcess" v-if="!processId">Process启动</ElButton>
          <ElButton type="danger" @click="onTestProcessStop" v-else>Process停止</ElButton>
        </div>
        <div b="1px solid gray">
          <div p4 b-b="1px solid gray">平台信息</div>
          <table class="my-table" style="width: 100%;">
            <tr v-for="item in installedInfo">
              <!-- <colgroup>
                <col />
                <col span="1" style="width: auto" />
              </colgroup> -->
              <td>{{ item.title }}</td>
              <td v-if="Object.prototype.toString.call(item.value) === '[object Array]'">
                <div v-for="subitem in item.value" my2>
                  {{ subitem }}
                </div>
              </td>
              <td v-else>{{ item.value }}</td>
                <!-- "
                v-for="(subitem, subindex) in item.value" -->
            </tr>
          </table>
        </div>
      </div>
    </ElCard>

    <el-card mb>
      <el-form label-width="auto">
        <p size="large">
          <span>选择目标窗口</span>
          <el-button @click="getWindowList">{{ windowsList.length === 0 ? '获取' : '刷新' }}</el-button>
        </p>
        <div w="600px" b="1px solid #ffffff40">
          <div v-for="item in windowsList" p4 b-b="1px solid #ffffff40" class="my-action" @click="textCopy(item.title)">
            {{ item.title }}
          </div>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { useClipboard, useLocalStorage } from '@vueuse/core';
import { defaultTaskManager, type SimpleResult } from '~/models/Task/TaskManager';
import type { ITitledData } from '~/models/interfaces';

function installHelper() {
  location.href="/download-helper.bat"
}

const installed = ref(false)
const testInterval = ref(0)
const processId = ref('')
const installedInfo = ref([] as ITitledData[])

onMounted(() => {
  const updateState = async () => {
    const responce = await fetch(baseUrl)
    const result = (await responce.json()) as SimpleResult
    if (result.success) {
      installed.value = true
      window.clearInterval(testInterval.value)
      installedInfo.value = result.message as any
    }
  }
  updateState()
  testInterval.value = window.setInterval(updateState, 10000)
})

async function onTestHelloworld() {
  const code = await loadCode('helloworld')
  const responce = await defaultTaskManager.value.execRaw(code)
}
async function onTestSleep() {
  const code = await loadCode('testSleep')
  const responce = await defaultTaskManager.value.execRaw(code)
}
async function onTestError() {
  const code = await loadCode('testError')
  const responce = await defaultTaskManager.value.execRaw(code)
}
async function onTestProcess() {
  const code = await loadCode('testProcess')
  const result = await defaultTaskManager.value.start(code)
  processId.value = result.message!
}

async function onTestProcessStop() {
  const responce = await defaultTaskManager.value.stop(processId.value)
  if (responce.success) {
    processId.value = ''
  }
}


const manual = ref(false)
const windowsList = ref([] as any[])

// const a = new TrainerProject()

async function getWindowList() {
  const code = await loadCode('get_all_windows')
  const wins = await defaultTaskManager.value.exec(code, true)

  windowsList.value = wins
}

function textCopy(txt: string) {
  useClipboard()
    .copy(txt)
    .then(() => {
      ElMessage({ type: 'success', message: '复制成功' })
    })

}
</script>

<style lang="scss" scoped>
</style>